#include "AnaDef.h"
#include "style.h"

TString fjpt;
Int_t fgrid = -999;
Int_t ftype = -999;
Bool_t fklogy = kFALSE;

const Int_t fColBl = kBlue;//7; //

const TString fmodname[]={"Perugia0","Perugia2011","Perugia2010NoCR"};//,"Perugia2010"};
//const TString ftypname[]={"pbarp","piPlusPiMinus","KplusKminus"};
const TString ftypname[]={"xg_p","xg_pi","xg_K"};
const TString foutname[]={"p", "#pi", "K"};

const Int_t fcol[]={kGray+2, kRed, fColBl, kGreen+3, kMagenta, kOrange+1, kOrange+2};
const Int_t fsty[]={22,20,26,24, 25};

void drawTheory(TGraphAsymmErrors * grstat, TGraphAsymmErrors * grsys, const Int_t nmod, TH1F * hmod[])
{
  style::SetGlobalStyle(0);

  Int_t idraw=0;

  TCanvas * cc = new TCanvas("cc","",600,400);

  style::PadSetup(cc);

  gStyle->SetOptTitle(0);
  gPad->SetRightMargin(0.01);
  gPad->SetTopMargin(0.03);
  gPad->SetLogy(fklogy);
  if(!fklogy) {
    grstat->SetMinimum(0);
  }
  else{
    Double_t grmin = 1e10;
    for(Int_t ii=0; ii<grstat->GetN(); ii++){
      if(grmin>grstat->GetY()[ii]){
        grmin = grstat->GetY()[ii];
      }
    }
    grstat->SetMinimum(grmin*0.5);
  }

  TLegend * lg=0x0;
  //pt
  if(fgrid/10==1){
    gPad->SetLogx();
    grstat->GetXaxis()->SetLimits(0.1, grstat->GetXaxis()->GetXmax()*1.01);
    grstat->GetXaxis()->SetMoreLogLabels();
    grstat->GetXaxis()->SetTitle("p_{T} (GeV/c)");

    if(fgrid%10==6){
      grstat->GetYaxis()->SetTitle(Form("1/N^{ch jet} dN_{%s}/dp_{T}^{%s} (GeV/c)^{-1}", foutname[ftype].Data(), foutname[ftype].Data()));
      if(ftype==0){
        if(fjpt.Contains("5-10")) grstat->SetMaximum(0.15);
        if(fjpt.Contains("10-15")) grstat->SetMaximum(0.15);
        if(fjpt.Contains("15-20")) grstat->SetMaximum(0.15);

        if(gPad->GetLogy()){
          lg=new TLegend(0.35, 0.2, 0.7, 0.5);
        }
        else{
          lg=new TLegend(0.15, 0.6, 0.5, 0.9);
        }
      }
      else if(ftype==1){
        if(fjpt.Contains("5-10")) grstat->SetMaximum(2.8);
        if(fjpt.Contains("10-15")) grstat->SetMaximum(3.8);
        if(fjpt.Contains("15-20")) grstat->SetMaximum(3.8);

        if(gPad->GetLogy()){
          lg=new TLegend(0.35, 0.2, 0.7, 0.5);
        }
        else{
          lg=new TLegend(0.6, 0.6, 0.9, 0.9);
        }
      }
      else if(ftype==2){
        if(fjpt.Contains("5-10")) grstat->SetMaximum(0.55);
        if(fjpt.Contains("10-15")) grstat->SetMaximum(0.3);
        if(fjpt.Contains("15-20")) grstat->SetMaximum(0.3);

        if(gPad->GetLogy()){
          lg=new TLegend(0.35, 0.2, 0.7, 0.5);
        }
        else{
          lg=new TLegend(0.15, 0.6, 0.5, 0.9);
        }
      }
      else{
        printf("bad ftype %d\n", ftype); exit(1);
      }
    }
  }
  //z
  else if(fgrid/10==2){
    grstat->GetXaxis()->SetLimits(0,1);
    grstat->GetXaxis()->SetTitle("z^{ch}");
    if(fgrid%10==6){
      grstat->GetYaxis()->SetTitle(Form("1/N^{ch jet} dN_{%s}/dz^{ch %s}", foutname[ftype].Data(), foutname[ftype].Data()));
    }

    if(ftype==0){
      if(fjpt.Contains("5-10")) grstat->SetMaximum(0.9);
      if(fjpt.Contains("10-15")) grstat->SetMaximum(1.5);
      if(fjpt.Contains("15-20")) grstat->SetMaximum(2.1);

      if(gPad->GetLogy()){
        lg=new TLegend(0.2, 0.2, 0.55, 0.5);
      }
      else{
        lg=new TLegend(0.6, 0.6, 0.9, 0.9);
      }
    }
    else if(ftype==1){
      if(fjpt.Contains("5-10")) grstat->SetMaximum(16);
      if(fjpt.Contains("10-15")) grstat->SetMaximum(28);
      if(fjpt.Contains("15-20")) grstat->SetMaximum(38);

      if(gPad->GetLogy()){
        lg=new TLegend(0.2, 0.2, 0.55, 0.5);
      }
      else{
        lg=new TLegend(0.6, 0.6, 0.9, 0.9);
      }
    }
    else if(ftype==2){
      if(fjpt.Contains("5-10")) grstat->SetMaximum(2.1);
      if(fjpt.Contains("10-15")) grstat->SetMaximum(3.5);
      if(fjpt.Contains("15-20")) grstat->SetMaximum(3.5);

      if(gPad->GetLogy()){
        lg=new TLegend(0.2, 0.2, 0.55, 0.5);
      }
      else{
        lg=new TLegend(0.6,0.6,0.9,0.9);
      }
    }
    else{
      printf("bad ftype %d\n", ftype); exit(1);
    }

  }
  else{
    printf("bad fgrid %d\n", fgrid);exit(1);
  }
  style::ResetStyle(lg);

  grstat->SetFillStyle(0);
  grsys->SetFillStyle(0);
  style::ResetStyle(grstat);
  style::ResetStyle(grsys);
  //=====================

  lg->SetHeader(Form("%s, pp 7 TeV p_{T}^{ch jet} %s GeV/c", foutname[ftype].Data(), fjpt.Data()));

  grstat->SetMarkerColor(fcol[idraw]); grstat->SetLineColor(fcol[idraw]); grstat->SetMarkerStyle(fsty[idraw]); grstat->SetMarkerSize(1);  grstat->Draw("ap");
  grsys->SetMarkerColor(fcol[idraw]);  grsys->SetLineColor(fcol[idraw]);  grsys->SetMarkerStyle(fsty[idraw]);  grsys->SetMarkerSize(1); idraw++;  lg->AddEntry(grsys,"ALICE","lfp");

  for(Int_t imod=0; imod<nmod; imod++){
    style::ResetStyle(hmod[imod]);
    hmod[imod]->SetMarkerColor(fcol[idraw]); hmod[imod]->SetLineColor(fcol[idraw]); hmod[imod]->SetMarkerStyle(fsty[idraw]); hmod[imod]->SetMarkerSize(1); hmod[imod]->SetLineWidth(2);  hmod[imod]->Draw("same hist C"); idraw++;  
    lg->AddEntry(hmod[imod],fmodname[imod].Data(),"l");//"lp");
  }
  grstat->Draw("p"); grsys->Draw("2"); 

  lg->Draw();

  cc->Print(Form("dthe_JPT%s_%s_GrID%d_logy%d.eps", fjpt.Data(), ftypname[ftype].Data(), fgrid, fklogy));
}

int main(int argc, char * argv[])
{
  for(Int_t ii=0; ii<argc; ii++){
    printf("%d: %s\n", ii, argv[ii]);
  }
  if(argc!=5){
    printf("argc!=5\n"); exit(1);
  }
  fjpt = argv[1];
  fgrid = atoi(argv[2]);
  ftype = atoi(argv[3]);
  fklogy = atoi(argv[4]);

  TString jname;
  if(fjpt.Contains("5-10")){
    jname = "05_10";
  }
  else if(fjpt.Contains("10-15")){
    jname = "10_15";
  }
  else if(fjpt.Contains("15-20")){
    jname = "15_20";
  }
  else{
    printf("bad fjpt %s\n", fjpt.Data()); exit(1);
  }

  TString xvar;
  if(fgrid==16){
    xvar="TrackPt";
  }
  else if(fgrid==26){
    xvar="Z";
  }
  else{
    printf("wrong fgrid %d\n", fgrid); exit(1);
  }

  //____________________

  TFile * corrRes = new TFile(Form("corrResults_JPT%s_GrID%d.root", fjpt.Data(), fgrid));
  if(!corrRes->IsOpen()){
    printf("no raw frac!! %s\n",  corrRes->GetName()); exit(1);
  }
  corrRes->ls();

  TGraphAsymmErrors * grstat=0x0;
  TGraphAsymmErrors * grsys=0x0;

  grstat = (TGraphAsymmErrors*) corrRes->Get(Form("grstat%d",ftype));
  if(!grstat){
    printf("no grstat %d\n", ftype); exit(1);
  }
  
  grsys = (TGraphAsymmErrors*) corrRes->Get(Form("grsys%d",ftype));
  if(!grsys){
    printf("no grsys %d\n", ftype); exit(1);
  }

  //____________________
  TFile * theoFile = new TFile("theory.root");
  if(!theoFile->IsOpen()){
    printf("no theoFile %s\n", theoFile->GetName()); exit(1);
  }
  theoFile->ls();

  const Int_t nmod = sizeof(fmodname)/sizeof(TString);

  TH1F * hmod[nmod];
  for(Int_t imod=0; imod<nmod; imod++){
    printf("imod %d/%d: %s\n", imod, nmod, fmodname[imod].Data());

    TDirectory *idir= theoFile->GetDirectory(Form("%sGen_%s", fmodname[imod].Data(), ftypname[ftype].Data()));
    if(!idir){
      printf("no idir!\n"); exit(1);
    }
    idir->ls();

    hmod[imod] = (TH1F*) idir->Get(Form("fh1FF%sGen_%s_%s", xvar.Data(), ftypname[ftype].Data(), jname.Data()));
    if(!hmod[imod]){
      printf("no hmod imod %d %s\n", imod, Form("fh1FF%sGen_%s_%s", xvar.Data(), ftypname[ftype].Data(), jname.Data())); exit(1);
    }
  }

  drawTheory(grstat, grsys, nmod, hmod);

  printf("drawTheorydonedone!\n");


  return 0;
}
